<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1"><title id="cb20941">CREATE SCHEMA</title><body><p id="sql_command_desc">Defines a new schema.</p><section id="section2"><title>Synopsis</title><codeblock id="sql_command_synopsis">CREATE SCHEMA <varname>schema_name</varname> [AUTHORIZATION <varname>username</varname>] 
   [<varname>schema_element</varname> [ ... ]]

CREATE SCHEMA AUTHORIZATION <varname>rolename</varname> [<varname>schema_element</varname> [ ... ]]

CREATE SCHEMA IF NOT EXISTS <varname>schema_name</varname> [ AUTHORIZATION <varname>user_name</varname> ]

CREATE SCHEMA IF NOT EXISTS AUTHORIZATION <varname>user_name</varname>
</codeblock></section><section id="section3"><title>Description</title><p><codeph>CREATE SCHEMA</codeph> enters a new schema into the current
database. The schema name must be distinct from the name of any existing
schema in the current database. </p><p>A schema is essentially a namespace: it contains named objects (tables,
data types, functions, and operators) whose names may duplicate those
of other objects existing in other schemas. Named objects are accessed
either by qualifying their names with the schema name as a prefix, or
by setting a search path that includes the desired schema(s). A <codeph>CREATE</codeph>
command specifying an unqualified object name creates the object in the
current schema (the one at the front of the search path, which can be
determined with the function <codeph>current_schema</codeph>). </p><p>Optionally, <codeph>CREATE SCHEMA</codeph> can include subcommands
to create objects within the new schema. The subcommands are treated
essentially the same as separate commands issued after creating the schema,
except that if the <codeph>AUTHORIZATION</codeph> clause is used, all
the created objects will be owned by that role.</p></section><section id="section4"><title>Parameters</title><parml><plentry><pt><varname>schema_name</varname></pt><pd>The name of a schema to be created. If this is omitted, the user name is used as the schema
            name. The name cannot begin with <codeph>pg_</codeph>, as such names are reserved for
            system catalog schemas. </pd></plentry><plentry><pt><varname>user_name</varname></pt><pd>The name of the role who will own the schema. If omitted, defaults
to the role running the command. Only superusers may create schemas
owned by roles other than themselves.</pd></plentry><plentry><pt><varname>schema_element</varname></pt><pd>An SQL statement defining an object to be created within the schema. Currently, only
              <codeph>CREATE TABLE</codeph>, <codeph>CREATE VIEW</codeph>, <codeph>CREATE
              INDEX</codeph>, <codeph>CREATE SEQUENCE</codeph>, <codeph>CREATE TRIGGER</codeph> and
              <codeph>GRANT</codeph> are accepted as clauses within <codeph>CREATE SCHEMA</codeph>.
            Other kinds of objects may be created in separate commands after the schema is
            created.<note>Greenplum Database does not support triggers.</note></pd></plentry>      
        <plentry>
          <pt><codeph>IF NOT EXISTS</codeph></pt>
          <pd>Do nothing (except issuing a notice) if a schema with the same name already exists.
              <varname>schema_element</varname> subcommands cannot be included when this option is
            used. </pd>
        </plentry>
</parml></section><section id="section5"><title>Notes</title><p>To create a schema, the invoking user must have the <codeph>CREATE</codeph>
privilege for the current database or be a superuser.</p></section><section id="section6"><title>Examples</title><p>Create a schema:</p><codeblock>CREATE SCHEMA myschema;</codeblock><p>Create a schema for role <codeph>joe</codeph> (the schema will also be named
  <codeph>joe</codeph>):</p><codeblock>CREATE SCHEMA AUTHORIZATION joe;</codeblock>  
      <p> Create a schema named <codeph>test</codeph> that will be owned by user
          <codeph>joe</codeph>, unless there already is a schema named <codeph>test</codeph>. (It
        does not matter whether <codeph>joe</codeph> owns the pre-existing schema.)
        <codeblock>CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;
</codeblock>
      </p>
</section><section id="section7"><title>Compatibility</title><p>The SQL standard allows a <codeph>DEFAULT CHARACTER SET</codeph> clause
in <codeph>CREATE SCHEMA</codeph>, as well as more subcommand types than
are presently accepted by Greenplum Database. </p><p>The SQL standard specifies that the subcommands in <codeph>CREATE
SCHEMA</codeph> may appear in any order. The present Greenplum Database
implementation does not handle all cases of forward references in subcommands;
it may sometimes be necessary to reorder the subcommands in order to
avoid forward references. </p><p>According to the SQL standard, the owner of a schema always owns all
objects within it. Greenplum Database allows schemas to contain objects
owned by users other than the schema owner. This can happen only if the
schema owner grants the <codeph>CREATE</codeph> privilege on the schema
to someone else, or a superuser chooses to create objects in it.</p>
      <p>The <codeph>IF NOT EXISTS</codeph> option is a Greenplum Database extension.</p></section><section id="section8"><title>See Also</title><p><codeph><xref href="ALTER_SCHEMA.xml#topic1" type="topic" format="dita"/></codeph>, <codeph><xref
            href="./DROP_SCHEMA.xml#topic1" type="topic" format="dita"/></codeph></p></section></body></topic>
